{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sea Creatures\n", "\n", "Our goal is to make a standard type of creature, and then have variations.\n", "\n", "We can have slight variations in a single class, but when you want to have many differences, you might want to have completely different classes.\n", "\n", "First, we make a Base Class of a creature. Let's call it `BaseCreature` and put it in a file called `BaseCreature.java`:\n", "\n", "You can get it from here:\n", "\n", "* [BaseCreature.java](https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS110%20Intro%20to%20Computing/2015-Fall/Notes/BaseCreature.java)\n", "\n", "or with:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS110%20Intro%20to%20Computing/2015-Fall/Notes/BaseCreature.java" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS110 Intro to Computing/2015-Fall/Notes/BaseCreature.java'.\n" ] } ], "source": [ "%%file BaseCreature.java\n", " \n", "class BaseCreature {\n", " // Properties of the BaseCreature:\n", " String state;\n", " float x, y, width, height;\n", " // Limits of movement:\n", " float min_x, max_x, min_y, max_y;\n", " // Other creatures:\n", " BaseCreature[] others;\n", " \n", " // Constructor:\n", " BaseCreature(float x, float y, \n", " float width, float height,\n", " float min_x, float max_x, \n", " float min_y, float max_y) {\n", " this.state = \"alive\";\n", " this.x = x;\n", " this.y = y;\n", " this.width = width;\n", " this.height = height;\n", " this.min_x = min_x;\n", " this.max_x = max_x;\n", " this.min_y = min_y;\n", " this.max_y = max_y;\n", " }\n", " \n", " void setOthers(BaseCreature[] others) {\n", " this.others = others;\n", " }\n", " float distance(BaseCreature other) {\n", " return sqrt(pow(int(this.x - other.x), 2) + pow(int(this.y - other.y), 2));\n", " }\n", " // Common methods:\n", " void move() {\n", " }\n", " void draw() {\n", " }\n", " void die() {\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extension\n", "\n", "To use the `BaseCreature`, we extend it (like we did with the Robot class). You can create a specific type of creature by extending the `BaseCreature`." ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS110 Intro to Computing/2015-Fall/Notes/Fish.java'.\n" ] } ], "source": [ "%%file Fish.java\n", " \n", "class Fish extends BaseCreature {\n", " //Properties of Fish:\n", " color mycolor;\n", " String direction;\n", " float speed;\n", " \n", " // Fish constructor:\n", " Fish(float x, float y, \n", " float width, float height,\n", " float min_x, float max_x, \n", " float min_y, float max_y,\n", " color mycolor, float speed) {\n", " // Initialize the BaseCreature:\n", " super(x, y, width, height, min_x, max_x, min_y, max_y);\n", " // Set the fish-specific properties:\n", " this.mycolor = mycolor;\n", " this.direction = \"left\";\n", " this.speed = speed;\n", " }\n", " \n", " // Overload base methods:\n", " void move() {\n", " if (this.state == \"alive\") {\n", " if (this.direction == \"left\") {\n", " this.x -= this.speed;\n", " } else if (this.direction == \"right\") {\n", " this.x += this.speed;\n", " }\n", " } \n", " if (this.x < this.min_x)\n", " this.direction = \"right\";\n", " if (this.x > this.max_x)\n", " this.direction = \"left\";\n", " }\n", " \n", " void draw() {\n", " if (this.state == \"alive\") {\n", " fill(this.mycolor);\n", " ellipse(this.x, this.y, this.width, this.height);\n", " } else {\n", " fill(color(128));\n", " ellipse(this.x, this.y, this.height, this.width);\n", " }\n", " }\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instances\n", "\n", "Now we can create multiple instances of the Fish. Note that creature1's and creature2's type is `BaseCreature` not `Fish`:" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_42\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_42\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_42\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_42\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #42:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #42 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include BaseCreature.java\n", "%include Fish.java\n", "\n", "BaseCreature creature1;\n", "BaseCreature creature2;\n", "\n", "void setup() {\n", " size(500, 500);\n", " creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);\n", " creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);\n", "}\n", "\n", "void draw() {\n", " background(color(0, 0, 255));\n", " // Move the creatures:\n", " creature1.move();\n", " creature2.move();\n", " // Draw the creatures:\n", " creature1.draw();\n", " creature2.draw();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Types of Creatures\n", "\n", "We can also create other creatures, extending `BaseCreature` in the same way:" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS110 Intro to Computing/2015-Fall/Notes/BlowFish.java'.\n" ] } ], "source": [ "%%file BlowFish.java\n", " \n", "class BlowFish extends BaseCreature {\n", " //Properties of BlowFish:\n", " String direction;\n", " float speed;\n", " \n", " // Fish constructor:\n", " BlowFish(float x, float y, \n", " float width, float height,\n", " float min_x, float max_x, \n", " float min_y, float max_y,\n", " float speed) {\n", " // Initialize the BaseCreature:\n", " super(x, y, width, height, min_x, max_x, min_y, max_y);\n", " // Set the fish-specific properties:\n", " this.direction = \"up\";\n", " this.speed = speed;\n", " }\n", " \n", " // Overload base methods:\n", " void move() {\n", " if (this.state == \"alive\") {\n", " if (this.direction == \"up\") {\n", " this.y -= this.speed;\n", " } else if (this.direction == \"down\") {\n", " this.y += this.speed;\n", " }\n", " } \n", " if (this.y < this.min_y)\n", " this.direction = \"down\";\n", " if (this.y > this.max_y)\n", " this.direction = \"up\";\n", " \n", " // if near other, kill them:\n", " if (this.others != null && this.state == \"alive\") {\n", " for (int i=0; i" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #45:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #45 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include BaseCreature.java\n", "%include Fish.java\n", "%include BlowFish.java\n", "\n", "BaseCreature creature1;\n", "BaseCreature creature2;\n", "BaseCreature creature3;\n", "\n", "void setup() {\n", " size(500, 500);\n", " creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);\n", " creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);\n", " creature3 = new BlowFish(250, 400, 100, 50, 400, 500, 100, 400, 1);\n", "}\n", "\n", "void draw() {\n", " background(color(0, 0, 255));\n", " // Move the creatures:\n", " creature1.move();\n", " creature2.move();\n", " creature3.move();\n", " // Draw the creatures:\n", " creature1.draw();\n", " creature2.draw();\n", " creature3.draw();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Death from Another\n", "\n", "And finally, allow them to be able to eat each other:" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_47\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_47\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_47\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_47\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #47:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #47 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include BaseCreature.java\n", "%include Fish.java\n", "%include BlowFish.java\n", "\n", "BaseCreature creature1;\n", "BaseCreature creature2;\n", "BaseCreature creature3;\n", "\n", "void setup() {\n", " size(500, 500);\n", " creature1 = new Fish(100, 100, 50, 25, 100, 400, 0, 500, color(255, 0, 0), 5);\n", " creature2 = new Fish(400, 300, 50, 25, 400, 500, 0, 500, color(0, 255, 0), 1);\n", " creature3 = new BlowFish(250, 400, 100, 50, 400, 500, 100, 400, 1);\n", " \n", " // Make an array with all:\n", " BaseCreature[] all = {creature1, creature2, creature3};\n", " \n", " // Set the others:\n", " creature1.setOthers(all);\n", " creature2.setOthers(all);\n", " creature3.setOthers(all);\n", "}\n", "\n", "void draw() {\n", " background(color(0, 0, 255));\n", " // Move the creatures:\n", " creature1.move();\n", " creature2.move();\n", " creature3.move();\n", " // Draw the creatures:\n", " creature1.draw();\n", " creature2.draw();\n", " creature3.draw();\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }